import { createRouter, createWebHashHistory } from 'vue-router'
import DeptListView from '../views/DeptList.vue'
import DocListView from '../views/DoctorList.vue'
import PlanListView from '../views/PlanList.vue'
import LoginView from '../views/LoginView.vue'
import BookView from '../views/BookView.vue'
import MyBooksView from '../views/MyBooks.vue'

const routes = [
  {
    path: '/login',
    name: 'login',
    component: LoginView
  },
  {
    path: '/',
    name: 'index',
    component: DeptListView
  },
  {
    path: '/doctors',
    name: 'doctors',
    component: DocListView
  },
  {
    path: '/plan',
    name: 'plan',
    component: PlanListView 
  },
  {
    path: '/book',
    name: 'book',
    component: BookView,
    meta:{
      needLogin:true
    }
  },
  {
    path: '/mybooks',
    name: 'mybooks',
    component: MyBooksView,
    meta:{
      needLogin:true
    }
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach(
  (to,from,next)=>{
    console.log("to:",to)
    if(to.meta.needLogin){//判断是否有必须登录的帽子
      if(sessionStorage.getItem('token'))//判断一下是否有登录凭证
          next();//正常放行
      else{
          sessionStorage.setItem("returnUrl",to.fullPath);//记录下登录后想返回的页面
          alert("请登录!");
          next({path:"/login"})//没有jwt_token就跳转到登录页
      }
    }else{
        next();
    }

  }
);


export default router
